﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FCBLL;
using FCDAL;
using FCModel;
using System.IO;
using System.Data;
using MiscLib;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
//using Microsoft.Office.Interop.Excel;

namespace FileConverter.BOMConverter
{
    public partial class WebBOMConverter : System.Web.UI.Page
    {
        string dataFormatType;
        string BomType = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //BomType = Request.Params["BOMType]"].ToString();
            //BOM_Head btt = CommonBLL.getBOM_HeadByBOMType(BomType);
            //iBomType = string.IsNullOrWhiteSpace(btt.BOMType.ToString()) ? iBomType : btt.BOMType;
            //dataFormatType = new BaseRepositoryDAL<bas_TestType>().LoadEntities(p => p.testType.Equals(TestType, StringComparison.OrdinalIgnoreCase)).FirstOrDefault().dataFormatType;
            //dataFormatType = new BaseRepositoryDAL<BOM_Head>().LoadEntities(p => p.BOMType.Equals(BomType)).FirstOrDefault();
            lblInfo.Text = "";
            if (!Page.IsPostBack)
            {
                DropDownList.DataSource = new BaseRepositoryDAL<BOM_Head>().LoadEntities(p => p.BOMType != null).Select(p => p.BOMType).Distinct().ToList<string>();
                DropDownList.DataBind();
                DropDownList.Items.Insert(0, new ListItem(""));
            }
        }
        protected void Button_Click(object sender, EventArgs e)
        {
            if (DropDownList.SelectedValue.ToString() == "")
            {
                lblInfo.Text = "没有指定BOM类型.";
                return;
            }
            string logPath = Server.MapPath("~/Excel/");
            if (FileUpload.PostedFile.FileName == "" || FileUpload.PostedFile.InputStream.Length < 1)
            {
                lblInfo.Text = "没有指定BOM文件.";
                return;
            }
            if (FileUploadPOS.PostedFile.FileName == "" || FileUploadPOS.PostedFile.InputStream.Length < 1)
            {
                lblInfo.Text = "没有指定坐标文件.";
                return;
            }
            else
            {
                try
                {
                    //根据testType获取dataFormatType
                    string fileExt = System.IO.Path.GetExtension(FileUpload.FileName).ToLower();
                    string FileName = new FileInfo(FileUpload.PostedFile.FileName).Name;
                    TableFormatBOMBLL ifbll = null;
                    string[] arrFile = null;
                    string filePOS = System.IO.Path.GetExtension(FileUploadPOS.FileName).ToLower();
                    string POSFileName = new FileInfo(FileUploadPOS.PostedFile.FileName).Name;
                    string[] arrPOSFile = null;

                    //CommonBLL.WriteLog(logPath, "\r\n");
                    //CommonBLL.WriteLog(logPath, "进入程序");
                    if (filePOS.Contains(".xls") || filePOS.Contains(".xlsx") || filePOS.Contains(".csv"))// == ".xlsx" || fileExt == ".xls")
                    {
                        string POS_FileName = Guid.NewGuid().ToString() + filePOS;
                        if (!Directory.Exists(Server.MapPath("~/Excel/")))
                        {
                            Directory.CreateDirectory(Server.MapPath("~/Excel/"));
                        }
                        //FileUploadPOS.PostedFile.SaveAs(Server.MapPath("~/CSVResonance/") + POS_FileName);
                        //DataTable dt = CommonBLL.CSVTODS(Server.MapPath("~/CSVResonance/") + POS_FileName, 0, Encoding.GetEncoding("GB2312"));

                        FileUploadPOS.PostedFile.SaveAs(Server.MapPath("~/Excel/") + POS_FileName);
                        DataSet ds = CommonBLL.ExcelToDSBysheets(Server.MapPath("~/Excel/") + POS_FileName);
                        if (System.IO.File.Exists(Server.MapPath("~/Excel/") + POS_FileName))
                        {
                            File.Delete(Server.MapPath("~/Excel/") + POS_FileName);
                        }
                        arrPOSFile = CommonBLL.DataSetToStringArrayNull(ds);
                    }
                    if (fileExt.Contains(".xls") || fileExt.Contains(".xlsx") || fileExt.Contains(".csv"))// == ".xlsx" || fileExt == ".xls")
                    {
                        string fileName = Guid.NewGuid().ToString() + fileExt;
                        if (!Directory.Exists(Server.MapPath("~/Excel/")))
                        {
                            Directory.CreateDirectory(Server.MapPath("~/Excel/"));
                        }
                        FileUpload.PostedFile.SaveAs(Server.MapPath("~/Excel/") + fileName);
                        DataSet ds = CommonBLL.ExcelToDSBysheets(Server.MapPath("~/Excel/") + fileName);

                        if (System.IO.File.Exists(Server.MapPath("~/Excel/") + fileName))
                        {
                            File.Delete(Server.MapPath("~/Excel/") + fileName);
                        }
                        arrFile = CommonBLL.DataSetToStringArrayNull(ds);
                    }
                    ifbll = new TableFormatBOMBLL();
                    //BomType = "HQBOM";
                    BomType = DropDownList.SelectedValue.ToString();
                    //EXCEL转换表头
                    IList<BOMParameterDao> listSSHead = ifbll.AnalysisHead(BomType);
                    //EXCEL转换表头的值
                    IList<BOMParameterDao> listSS = ifbll.AnalysisHeadFile(arrFile, arrPOSFile, BomType);
                    //EXCEL转换填充的值
                    IList<BOMParameterDao> listSSD = ifbll.AnalysisFile(arrFile,arrPOSFile,BomType);
                    //    CommonBLL.WriteLog(logPath, "分析完成");
                    lblInfo.Text = "分析完成";
                    DataTable dt = CommonBLL.BOMParameterDaoToDataTable(listSSD, BomType);
                   // DataTable dt = CommonBLL.ExcelToDS(listSSD,"YES",2);
                    string path = Server.MapPath("~/Excel/");
                    ExporttoExcel(dt, listSSHead,listSS, path, BomType, FileName.Split('.')[0], logPath);

                    //  CommonBLL.WriteLog(logPath, "转换成EXCEL，成功下载");
                }
                catch (Exception err)
                {
                    lblInfo.Text = err.Message;
                    CommonBLL.WriteLog(logPath, err.Message);
                }
            }
        }
        private void ExporttoExcel(System.Data.DataTable dt, IList<BOMParameterDao> listSSHead, IList<BOMParameterDao> list, string path, string BomType, string FileName, string logPath)
        {
            ExcelExport.OutputExcelBOM(dt.DefaultView, listSSHead,list, path, BomType, FileName, HttpContext.Current.Response, Server, logPath);
        }
    }
}